//
//  GCMSheBeiRequst.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/11/15.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMSheBeiRequst.h"
#import "GCMEQUShouYeModel.h"
#import "GCMSBHisModel.h"
#import "GCMSBPlanModel.h"
#import "GCMSHEBEIDetailModel.h"
#import "GCMOtherModel.h"

@implementation GCMSheBeiRequst

+ (NSURLSessionDataTask *)postXQOtherDataWithSBId:(NSString *)areaId WithURL:(NSString *)url Success:(void (^) (id obj , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"sbid"] = areaId;
    return [[CZHAppNetAPIClient sharedClient] POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"+nihao +1+  %@",dict);
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict valueForKey:@"data"];
        //        计划
        NSMutableArray *PlanArray = [[NSMutableArray alloc]init];
            for (NSDictionary *dict1 in reciveListArray) {
                GCMOtherModel *listModel = [[GCMOtherModel alloc]initWithDictionary:dict1 error:nil];
                listModel.idd = [dict1 valueForKey:@"id"];
//                NSLog(@"%@",listModel);
                [PlanArray addObject:listModel];
//                NSLog(@"---%@",PlanArray);
            }

        NSLog(@"++2+  %@",reciveListArray);
        if (success) {
            success(PlanArray,nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];

    
}

+ (NSURLSessionDataTask *)postSheBeiDataWithParmerts:(NSDictionary *)parmerts WithUrl:(NSString *)url Success:(void (^) (id obj , NSError *))success{
    
    return [[CZHAppNetAPIClient sharedClient] POST:url parameters:parmerts success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *str=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"*1%@",dict);
        if (success) {
            success(dict,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //        NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
}

+ (NSURLSessionDataTask *)postScanDataWithSBId:(NSString *)areaId WithURL:(NSString *)url WithType:(NSString *)type Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"id"] = areaId;
        NSLog(@"+nihao +1+  %@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+nihao +1+  %@",dict);

        NSMutableArray *Array = [[NSMutableArray alloc]init];
//        历史
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
//        计划
        NSMutableArray *PlanArray = [[NSMutableArray alloc]init];
        Array = [dict objectForKey:@"data"];
        if ([type isEqualToString:@"Plan"]) {
            for (NSDictionary *dict1 in Array) {
                GCMSBPlanModel *listModel = [[GCMSBPlanModel alloc]initWithDictionary:dict1 error:nil];
                NSLog(@"%@",listModel);
                [PlanArray addObject:listModel];
                //NSLog(@"---%@",ReciveListArray);
            }
        }else{
            for (NSDictionary *dict1 in Array) {
                GCMSBHisModel *lisModel = [[GCMSBHisModel alloc]initWithDictionary:dict1 error:nil];
                NSLog(@"%@",lisModel);
                [ReciveListArray addObject:lisModel];
                //NSLog(@"---%@",ReciveListArray);
            }
        }

        if (success) {
            if ([type isEqualToString:@"Plan"]) {
                success(PlanArray,nil);
            }else{
                success(ReciveListArray,nil);
            }
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postBaseInfoWithsbId:(NSString *)sbId Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"sbid"] = sbId;
    return [[CZHAppNetAPIClient sharedClient] POST:SHEBEJBXX_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                NSLog(@"+nihao +1+  %@",dict);
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict valueForKey:@"data"];
        if (success) {
            success(reciveListArray,nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postDetailWithsbId:(NSString *)sbId Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"sbid"] = sbId;
    return [[CZHAppNetAPIClient sharedClient] POST:SHEBEJBXQ_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+nihao +1+  %@",dict);
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        GCMSHEBEIDetailModel *listModel = [[GCMSHEBEIDetailModel alloc]initWithDictionary:[CZHChangeNullString getValueFromDictionary:[dict valueForKey:@"data"]] error:nil];
        [ReciveListArray addObject:listModel];
//        NSLog(@"%@",ReciveListArray);
        if (success) {
            success(ReciveListArray,nil);            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postSheBeiDataWithaId:(NSString *)aId WithType:(NSString *)type WithstartDate:(NSString *)startDate WithendData:(NSString *)endDate WithPage:page Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"a_id"] = aId;
    parameters[@"type"] = type;
    parameters[@"startDate"] = startDate;
    parameters[@"endDate"] = endDate;
    parameters[@"page"] = page;
    NSLog(@"++1+房间信息%@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:SHEBEILIST_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"1111%@",dict);
        
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict objectForKey:@"data"];
        NSLog(@"/////%@",reciveListArray);
        for (NSDictionary *dict1 in reciveListArray) {
            
            GCMEQUShouYeModel *listModel = [[GCMEQUShouYeModel alloc]initWithDictionary:[CZHChangeNullString getValueFromDictionary:dict1] error:nil];
            NSLog(@"%@",listModel);
            [ReciveListArray addObject:listModel];
        }
//         NSLog(@"++1+房间信息  %@",responseObject);
        if (success) {
            success(ReciveListArray,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
    
}


+ (NSURLSessionDataTask *)postScanDataWithId:(NSString *)areaId Success:(void (^) (id obj , NSError *))success{
    
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"id"] = areaId;
//        NSLog(@"+nihao +1+  %@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:SHEBEISCAN_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"+nihao +1+  %@",[self dictionaryWithJsonString:[dict valueForKey:@"data"]]);
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        [reciveListArray addObject:[self dictionaryWithJsonString:[dict valueForKey:@"data"]]];
        if (success) {
            success(reciveListArray,nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];

}
+ (NSURLSessionDataTask *)postScanDataSuccess:(void (^) (id obj , NSError *))success{
    return [[CZHAppNetAPIClient sharedClient] POST:SHEBEITYPE_URL parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict valueForKey:@"data"];
        if (success) {
            success(reciveListArray,nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}

+ (NSURLSessionDataTask *)postoutlineSheBeiDataWithaId:(NSString *)aId WithstartDate:(NSString *)startDate Success:(void (^) (id obj , NSError *))success{
    NSMutableDictionary *parameters= [[NSMutableDictionary alloc]init];
    
    parameters[@"a_id"] = aId;
    parameters[@"startDate"] = startDate;
    NSLog(@"++1+房间信息%@",parameters);
    return [[CZHAppNetAPIClient sharedClient] POST:SBOUTLINELIST_URL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"1111%@",dict);
        
        NSMutableArray *ReciveListArray = [[NSMutableArray alloc]init];
        NSMutableArray *reciveListArray = [[NSMutableArray alloc]init];
        reciveListArray = [dict objectForKey:@"data"];
//        NSLog(@"/////%@",reciveListArray);
        for (NSDictionary *dict1 in reciveListArray) {
            
            GCMEQUShouYeModel *listModel = [[GCMEQUShouYeModel alloc]initWithDictionary:[CZHChangeNullString getValueFromDictionary:dict1] error:nil];
            NSLog(@"%@",listModel);
            [ReciveListArray addObject:listModel];
        }
        //         NSLog(@"++1+房间信息  %@",responseObject);
        if (success) {
            success(ReciveListArray,nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //NSLog(@"++2+  %@",error);
        if (success) {
            success(nil, error);
        }
    }];
    
}




+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


@end
